class Company < ActiveRecord::Base
  
  has_many :incumbents
  has_many :comments,:foreign_key=>"obj_id", :conditions=>"category_id=3"
  has_many :indices, :foreign_key=>"obj_id", :conditions=>"category_id=3"
  
  belongs_to :report_industry, :class_name=>"Industry",:foreign_key => "industry_id"
  belongs_to :view_industry, :class_name=>"Industry", :foreign_key => "tindustry_id"
  belongs_to :report_entity, :class_name=>"Entity",:foreign_key => "entity_id"
  belongs_to :view_entity, :class_name=>"Entity", :foreign_key => "tentity_id"
  belongs_to :report_size, :class_name=>"Size", :foreign_key => "size_id"
  belongs_to :view_size, :class_name=>"Size", :foreign_key => "tsize_id"
  belongs_to :picture, :foreign_key =>"picture_id"
  belongs_to :user, :foreign_key=>"user_id"
  belongs_to :report, :class_name => "Report::CompanyReport",:foreign_key => "company_report_id"
  
  
  
  def before_create
    self.incumbents_count=0
    self.deleted=0
    self.view_times=0
    self.reply_times=0
    self.created_at= Time.now
    self.updated_at= Time.now
    self.picture_id=2
    self.search_tag<<" "  unless self.search_tag==""
    self.copy_view_to_report
    self.create_search_tag
  end
  
  def after_create
    Index.index_search_tag(self)
  end
  
  def reindex_search_tag
    self.search_tag<< " "  unless self.search_tag==""
    self.create_search_tag
    Index.clear_search_tag(self)
    Index.index_search_tag(self)
    self.save
  end
  
  def tag_list
    return Index.show_tag_list(self)
  end
  
  def self.validate_params(params)
    validation=[]
    if params[:tgrade]
      validation<<["<li>请输入有效的公司名称</li>"] if params[:name]==""
      validation<<["<li>输入的公司名字太长了</li>"] if params[:name] && params[:name].length>100
      validation<<["<li>请选择公司所属的行业</li>"] unless params[:tindustry_id].to_i.integer? && params[:tindustry_id].to_i>0 && params[:tindustry_id].to_i<=Industry.count
      validation<<["<li>请选择公司的企业性质</li>"] unless params[:tentity_id].to_i.integer? && params[:tentity_id].to_i>0 && params[:tentity_id].to_i<=Entity.count
      validation<<["<li>请输入公司的内部级别</li>"] unless params[:tgrade].to_i.integer? && params[:tgrade].to_i>0
      validation<<["<li>请选择公司规模</li>"] unless params[:tsize_id].to_i.integer? && params[:tsize_id].to_i>0 && params[:tsize_id].to_i<=Size.count
      validation<<["<li>公司地址太长了</li>"] if params[:address]&&params[:address].length>300
      validation<<["<li>电话号码不正确</li>"] if params[:telephone]&&(params[:telephone].length>300||params[:telephone]=~/\D./)
      unless params[:website_address]=="" || params[:website_address]==nil 
        validation<<["<li>网站地址太长了</li>"] if params[:website_address].length>300
        validation<<["<li>网站地址不正确</li>"] unless params[:website_address]=~/\w+\.+\w*(com|net|org|cn)\w*/
      end
      validation<<["<li>关键字太长了</li>"] if params[:search_tag]&&params[:search_tag].length>300
    else
      validation<<["<li>请选择或创建公司信息</li>"]
    end
    return validation
  end
  
  def create_search_tag
    self.search_tag << self.name + " "
    self.search_tag << self.report_industry.name + " "
    self.search_tag << self.report_entity.name  
  end
  
  def copy_report_to_view
    self.view_industry=self.report_industry
    self.view_entity=self.report_entity
    self.view_size=self.report_size
    self.tgrade=self.grade
    self.updated=1
  end
  
  def copy_view_to_report
    self.report_industry=self.view_industry
    self.report_entity=self.view_entity
    self.report_size=self.view_size
    self.grade=self.tgrade
    self.updated=1
  end
  
end
